package com.nuoniu.sibanyun.controller.cms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.cms.CmsProduct;
import com.nuoniu.sibanyun.service.cms.ICmsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 网站产品 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/cms/product")
@Api(tags = "产品管理")
public class CmsProductController extends BaseController {

    @Autowired
    private ICmsProductService productService;

    @ApiOperation("获取列表")
    @GetMapping("pageList")
    public HttpResult pageList(Page page, CmsProduct product) {
        return HttpResult.ok(productService.pageList(page, product));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody CmsProduct product) {
        if(product == null || StringUtils.isBlank(product.getName())) {
            return HttpResult.error("产品名称不能为空");
        }

        productService.saveProduct(product);
        return HttpResult.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        productService.removeById(id);
        return HttpResult.ok();
    }

}
